<template>
  <div>
    <div>
      <el-button class="right" type="primary" @click="dialogFormVisible1 = true">新增</el-button>

        <el-dialog title="新增" :visible.sync="dialogFormVisible1"  width="50%">
          <el-form :inline="true" :model="addList">
            <el-form-item label="用户账号" :label-width="formLabelWidth">
              <el-input v-model="addList.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户名称" :label-width="formLabelWidth">
              <el-input v-model="addList.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户密码" :label-width="formLabelWidth">
              <el-input v-model="addList.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" :label-width="formLabelWidth">
              <el-input v-model="addList.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="启用状态" :label-width="formLabelWidth" class="dis">
              <el-select v-model="addList.state">
                <el-option label="正常" :value="1"></el-option>
                <el-option label="冻结" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible1 = false">取 消</el-button>
            <el-button type="primary" @click="dialogFormVisible1 = false,addUser(addList)">确 定</el-button>
          </div>
        </el-dialog>


      <el-form :inline="true" :model="input1" class="demo-form-inline">
        <el-form-item label="用户名称">
          <el-input v-model="input1.name"></el-input>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-select v-model="input1.state" clearable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="冻结" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="selectByPage(pageInfo.current,pageInfo.size)">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
        :data="pageInfo.records"
        border
        style="width: 100%">
      <el-table-column
          prop="username"
          label="用户账号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="用户名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱">
      </el-table-column>
      <el-table-column
          prop="leavedate"
          label="最后登录时间">
      </el-table-column>
      <el-table-column  label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">正常</span>
          <span v-else>冻结</span>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="updState(scope.row.id)">变更状态</el-button>
          |
          <el-button type="text" @click="upduserList(scope.row)" >修改</el-button>
          <el-dialog title="用户信息修改" :visible.sync="dialogFormVisible" :modal-append-to-body=false width="50%">
            <el-form :inline="true" :model="userList">
              <el-form-item label="id" :label-width="formLabelWidth">
                <el-input :disabled="true" v-model="userList.id" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="用户账号" :label-width="formLabelWidth">
                <el-input v-model="userList.username" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="用户名称" :label-width="formLabelWidth">
                <el-input v-model="userList.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" :label-width="formLabelWidth">
                <el-input v-model="userList.email" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="启用状态" :label-width="formLabelWidth">
                <el-select v-model="userList.state">
                  <el-option label="正常" :value="1"></el-option>
                  <el-option label="冻结" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="dialogFormVisible = false,updUser(userList)">确 定</el-button>
            </div>
          </el-dialog>
          &nbsp;&nbsp;|
          <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          |
          <el-button type="text" @click="getByLog(scope.row.id)">log日志</el-button>
          <el-dialog title="登陆日志" :visible.sync="dialogTableVisible" :modal-append-to-body=false>
            <el-table :data="logList">
              <el-table-column property="ip" label="ip地址" width="150"></el-table-column>
              <el-table-column property="time" label="登录时间" width="200"></el-table-column>
              <el-table-column property="username" label="用户名"></el-table-column>
            </el-table>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page=this.pageInfo.current
          :page-sizes="[10,50,100]"
          :page-size=this.pageInfo.size
          layout="sizes, prev, pager, next, jumper,->,total"
          :total=this.pageInfo.total>
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Yhgl",
  data:function (){
    return{
      pageInfo:{
        size:10
      },
      dialogFormVisible : false,
      dialogFormVisible1:false,
      dialogTableVisible: false,
      userList:{},
      formLabelWidth:'100px',
      input1:{
        name:'',
        state:''
      },
      addList:{
        state:1
      },
      logList:{}
    }
  },
  methods:{
    selectByPage(current,size){
      this.$axios.post('/nb/user/getUser/'+current+'/'+size,this.input1).then(res=>{
        this.pageInfo=res.data.data;
      })
    },
    handleSizeChange(val) {
      this.selectByPage(this.pageInfo.current,val);
    },
    handleCurrentChange(val) {
      this.selectByPage(val,this.pageInfo.size);
    },
    upduserList(res){
      this.dialogFormVisible=true
      this.userList=res
    },
    updUser(res){
      this.$axios.post('/nb/user/upduser',res).then(res=>{
        this.$message({
          type: 'success',
          message: '更新成功!'
        });
        this.selectByPage(this.pageInfo.current,this.pageInfo.size);
      })
    },
    addUser(res){
      this.$axios.post('/nb/user/adduser',res).then(res=>{
        this.$message({
          type: 'success',
          message: '新增成功!'
        });
        this.selectByPage(this.pageInfo.current,this.pageInfo.size);
      })
    },
    del(userid) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post('/nb/user/deluser/'+userid).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.selectByPage(this.pageInfo.current,this.pageInfo.size);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updState(id){
      this.axios({
        method:"post",
        url:"/nb/user/updstate/"+id
      }).then((res)=>{
        this.$message({
          type: 'success',
          message: '更新成功!'
        });
        this.selectByPage(this.pageInfo.current,this.pageInfo.size);
      })
    },
    getByLog(id){
      this.axios({
        method:"post",
        url:"/nb/login-log/getById/"+id
      }).then((res)=>{
        this.logList=res.data.data
        this.dialogTableVisible=true
      })
    }
  },
  created(){
    this.selectByPage(1,10);
  }
}
</script>

<style scoped>
  .demo-form-inline{
    display: flex;
  }
  .right{
    display: flex;
    float: right;
  }
.dis{
  position: relative;
  left: -150px;
}
</style>
